package com.cy.shiro.config;

import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.pojo.SysUser;
import com.cy.shiro.service.SysResourcesService;
import com.cy.shiro.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourcesService sysResourcesService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject(); // 主体
        SysUser user = (SysUser) subject.getPrincipal();// 获取当前登陆用户

        //赋予权限
        List<SysResources> resourcesList = null;
        SysUser sysUser = sysUserService.findById(user.getId());
        if(user == null){
            return info;
        }

        //root用户默认拥有所有权限
        if(sysUser.getUsername() == "root"){
            resourcesList = sysResourcesService.listAll();
        } else {
            resourcesList = sysResourcesService.listByUserId(sysUser.getId());
        }
        //设置权限
        if(!CollectionUtils.isEmpty(resourcesList)){
            Set<String> permissionSet = new HashSet<>();
            for(SysResources resources : resourcesList){
                String permission = resources.getPermission();
                if(!StringUtils.isEmpty(permission)){
                    permissionSet.addAll(Arrays.asList(permission.trim().split(",")));
                }
            }
//            System.out.println("permissionSet:"+permissionSet);
            info.setStringPermissions(permissionSet);
        }

        //设置单个权限
//        info.addStringPermission(dbUser.get());

        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");

        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        System.out.println("用户名："+ token.getUsername()+",getName:"+getName());

        //查询用户信息
        SysUser user = sysUserService.findByUsername(token.getUsername());

        if(user == null || user.getStatus() == 0){
            return null;
        }
        //判断密码
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }
}
